import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  UseGuards,
  Request,
  HttpCode,
  HttpStatus,
} from '@nestjs/common';
import { UsersService } from './users.service';
import { CreateUserDto } from './dto/create-user.dto';
import { UpdateUserDto } from './dto/update-user.dto';
import { LoginUserDto } from './dto/login-user.dto';

@Controller('users')
export class UsersController {
  constructor(private readonly usersService: UsersService) {}

  @Post('register')
  async register(@Body() createUserDto: CreateUserDto) {
    const user = await this.usersService.create(createUserDto);
    return {
      message: '注册成功',
      user: {
        id: user.id,
        name: user.name,
        email: user.email,
        age: user.age,
        location: user.location,
      },
    };
  }

  @Post('login')
  @HttpCode(HttpStatus.OK)
  async login(@Body() loginUserDto: LoginUserDto) {
    const user = await this.usersService.validateUser(loginUserDto);
    // 这里应该生成JWT令牌，但现在先返回模拟数据
    return {
      message: '登录成功',
      token: `user_${Date.now()}_${Math.random().toString(36).substring(2, 15)}`,
      user: {
        id: user.id,
        name: user.name,
        email: user.email,
        age: user.age,
        location: user.location,
        interests: user.interests,
        bio: user.bio,
      },
    };
  }

  @Get('profile/:id')
  async getProfile(@Param('id') id: string) {
    const user = await this.usersService.findOne(+id);
    return {
      id: user.id,
      name: user.name,
      email: user.email,
      age: user.age,
      location: user.location,
      interests: user.interests,
      bio: user.bio,
      createdAt: user.createdAt,
    };
  }

  @Patch('profile/:id')
  async updateProfile(
    @Param('id') id: string,
    @Body() updateUserDto: UpdateUserDto,
  ) {
    const user = await this.usersService.update(+id, updateUserDto);
    return {
      message: '个人资料更新成功',
      user: {
        id: user.id,
        name: user.name,
        email: user.email,
        age: user.age,
        location: user.location,
        interests: user.interests,
        bio: user.bio,
      },
    };
  }

  @Post('change-password/:id')
  async changePassword(
    @Param('id') id: string,
    @Body() body: { currentPassword: string; newPassword: string },
  ) {
    const success = await this.usersService.changePassword(
      +id,
      body.currentPassword,
      body.newPassword,
    );
    return {
      message: success ? '密码修改成功' : '密码修改失败',
    };
  }
}
